<?

class Event {
	private $eventid;
	private $name;
	private $shortname;
	private $eventshort;
	private $year;
	private $week;
	private $thanks;
	private $location;
	private $latlong;
	private $website;
	private $official;
	
	// CONTRACT: __construct : number -> Event
	// PURPOSE
	// __construct is the auto-called function that builds a new Event. It takes a $eventid and returns the Event object for the corresponding event in the database.
	
	function __construct($event) {
		//ensure valid input
		$event = mysql_real_escape_string($event);
		if (!is_numeric($event)) {
		    //setup and run query
		    $year = substr($event, 0, 4);
		    $eventshort = substr($event, 4);
    		$eventinfo_query_filter = "SELECT * FROM events WHERE year='$year' AND eventshort='$eventshort' LIMIT 1";
    		$eventinfo_query = mysql_query($eventinfo_query_filter);
    		if (mysql_num_rows($eventinfo_query) < 1) {
    			throw_error("No event found for year/eventshort for Event Object instantiation. Given " . $year . $eventshort);
    		}
		} else {
    		//setup and run query
    		$eventinfo_query_filter = "SELECT * FROM events WHERE eventid='$event' LIMIT 1";
    		$eventinfo_query = mysql_query($eventinfo_query_filter);
    		if (mysql_num_rows($eventinfo_query) < 1) {
    			throw_error("No event found for eventid for Event Object instantiation. Given " . $event);
    		}
    	}
		//copy into object
		$eventinfo = mysql_fetch_array($eventinfo_query);
		$this->eventid 		= $eventinfo["eventid"];
		$this->name 		= $eventinfo["name"];
		$this->shortname 	= $eventinfo["shortname"];
		$this->eventshort 	= $eventinfo["eventshort"];
		$this->year 		= $eventinfo["year"];
		$this->week 		= $eventinfo["week"];
		$this->thanks 		= $eventinfo["thanks"];
		$this->location 	= $eventinfo["location"];
		$this->latlong 		= array($eventinfo["lat"],$eventinfo["long"]);
		$this->website 		= $eventinfo["website"];
		$this->official 	= $eventinfo["official"];
	}
	
	function getName() {
		return $this->name;
	}
	function getWeek() {
		return $this->week;
	}
	function getYear() {
		return $this->year;
	}
	function getEventID() {
		return $this->eventid;
	}
	function getLocation() {
		return $this->location;
	}
	function getEventShort() {
		return $this->eventshort;
	}
	function getWebsite() {
	    return $this->website;
	}
	function getCodeString() {
	    return $this->getYear() . $this->getEventShort();
	}
	
	// CONTRACT: linkto() : string -> string
	// PURPOSE
	// linkto() takes (optionally) a string, and returns an HTML formatted anchor tag linking to the event's specific page.
	function linkto($string = Null) {
		global $BASE_PATH;
		global $MOD_REWRITE_ON;
		if ($string == Null) $string = $this->getName() . " (" . $this->getYear() . ")";
		if ($MOD_REWRITE_ON) {
		    return "<a href=\"" . $BASE_PATH . "event/" . $this->getCodeString() . "\" title=\"" . $string . "\">" . $string . "</a>";
		} else {
		    return "<a href=\"" . $BASE_PATH . "event.php?event=" . $this->getCodeString() . "\" title=\"" . $string . "\">" . $string . "</a>";
		}
	}
	
	// CONTRACT: hasMatches() : null -> bool
	// PURPOSE
	// hasMatches determines if an event has any matches in the system. returns a boolean correspondingly.
	function hasMatches() {
		$has_matches_query_filter = "SELECT matches.complevel FROM events, matches WHERE events.eventid = matches.event AND events.eventid='$this->eventid' LIMIT 1";
		$has_matches_query = mysql_query($has_matches_query_filter);
		if (mysql_num_rows($has_matches_query) > 0) {
			return TRUE;
		} else {
			return FALSE;
		}
	}
	
	// CONTRACT: hasEliminations() : null -> bool
	// PURPOSE
	// hasEliminations determines if an event has any elims in the system. returns a boolean correspondingly.
	function hasEliminations() {
		$has_elims_query_filter = "SELECT matches.complevel FROM events, matches WHERE events.eventid = matches.event AND events.eventid='$this->eventid' AND matches.complevel > 10 LIMIT 1";
		$has_elims_query = mysql_query($has_elims_query_filter);
		if (mysql_num_rows($has_elims_query) > 0) {
			return TRUE;
		} else {
			return FALSE;
		}
	}
	
	// CONTRACT: getAllMatches() : number -> List of Matches
	// PURPOSE
	// getAllMatches() is a function that returns a list of full Match objects for the event based on a passed complevel, or all the matches
	function getAllMatches($complevel = Null) {
		//get IDs of matches of event
		if ($complevel > 0) {
			$matches_query_filter = "SELECT matchid FROM matches WHERE event=" . $this->getEventID() . " AND complevel=". $complevel ." ORDER BY matchnumber;";
		} else {
			$matches_query_filter = "SELECT matchid FROM matches WHERE event=" . $this->getEventID() . " ORDER BY complevel, matchnumber;";
		}
		$matches_query = mysql_query($matches_query_filter);
		
		//construct list
		$list_of_matches = array();
		while ($match = mysql_fetch_array($matches_query)) {
			$match_object = new Match($match["matchid"]);
			$list_of_matches[] = $match_object;
		}
		
		//send the list back.
		return $list_of_matches;
	}
	
	// CONTRACT: getAllTeams() : null -> List of Teams
	// PURPOSE
	// getAllTeams() is a function that returns a list of full Team objects for the event.
	function getAllTeams() {
		//get IDs of teams attending event
		$teams_query_filter = "SELECT team FROM attendance WHERE event=" . $this->eventid . " ORDER BY team;";
		$teams_query = mysql_query($teams_query_filter);
		
		//construct list
		$list_of_teams = array();
		while ($team = mysql_fetch_array($teams_query)) {
			if ($team["team"] > 0) {
				$team_object = new Team($team["team"]);
				$list_of_teams[] = $team_object;
			}
		}
		
		//send the list back.
		return $list_of_teams;
	}
	
	// CONTRACT: getTeamMatches() : Team -> List of Events
	// PURPOSE
	// getTeamMatches takes a Team, and returns a list of Matches in the Event that the Team played in.
	function getTeamMatches($team) {
		$teamnumber = $team->getTeamNumber();
		$matches_query_filter = "SELECT matchid FROM matches WHERE event=" . $this->getEventID() . " AND (red1 = " . $teamnumber . " OR red2 = " . $teamnumber . " OR red3 = " . $teamnumber . " OR blue1 = " . $teamnumber . " OR blue2 = " . $teamnumber . " OR blue3 = " . $teamnumber . ") ORDER BY complevel, matchnumber";
		$matches_query = mysql_query($matches_query_filter);
		
		//construct list
		$list_of_matches = array();
		while ($match = mysql_fetch_array($matches_query)) {
			if ($match["matchid"] > 0) {
				$match_object = new Match($match["matchid"]);
				$list_of_matches[] = $match_object;
			}
		}
		
		return $list_of_matches;
	}

	// CONTRACT : getTeamCount() : null -> number
	// PURPOSE
	// getTeamCount() returns the number of teams in attendance at an event.
	function getTeamCount() {
		$eventinfo_query_filter = "SELECT COUNT(*) as \"teamsregistered\", events.eventid FROM attendance, events WHERE events.eventid = '$this->eventid' AND attendance.event = events.eventid GROUP BY eventid;";
		$eventinfo_query = mysql_query($eventinfo_query_filter);
		$event = mysql_fetch_array($eventinfo_query);
		return $event["teamsregistered"];
	}
	
	// CONTRACT : getVideoCount() : null -> number
	// PURPOSE
	// getVideoCount() returns the number of videos online for this event.
	function getVideoCount() {
	    $videos_query_filter = "SELECT COUNT(matches.vidup) FROM matches WHERE matches.event='$this->eventid' AND (matches.flvup = 1 OR matches.vidup = 1)";
		$videos_query = mysql_query($videos_query_filter);
		$videosonline = mysql_fetch_array($videos_query);
		$videosonline = $videosonline[0];
		return $videosonline;
	}
	
}

?>